package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserService userService;


    @PostMapping(value = "/fans/{uid}",name="喜欢")
    public ResponseEntity likeFans(@PathVariable("uid") Long fansUserId) throws Exception{
        return   userService.likeFans(fansUserId);
    }



    @DeleteMapping(value = "/like/{uid}",name="取消喜欢")
    public ResponseEntity deleteFans(@PathVariable("uid") Long fansUserId) throws Exception{
        return   userService.deleteFans(fansUserId);
    }

    @GetMapping(value = "/friends/{type}",name="互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表")
    public ResponseEntity queryUserLikeList(@PathVariable("type") Integer type,
                                            @RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10")  Integer pagesize) throws Exception{
        return   userService.queryUserLikeList(type,page,pagesize);
    }

    @GetMapping(value = "/counts",name="互相喜欢，喜欢，粉丝 - 统计")
    public ResponseEntity counts() throws Exception{
        return   userService.queryCounts();
    }

    @PostMapping(value = "/header",name="更新用户头像")
    public ResponseEntity updateUserInfoHead(MultipartFile headPhoto) throws Exception{
        return   userService.loginReginfoHead(headPhoto);
    }

    @PutMapping(name="更新用户资料")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
      return   userService.updateUserInfo(userInfo);
    }



    @GetMapping(name = "读取用户信息")
    public ResponseEntity queryUserInfo(@RequestParam(required = false) Long userID, @RequestParam(required = false)Long huanxinID, @RequestHeader("Authorization") String token){
//        就是根据用户id查询用户信息而已，但是现在有三个查询用户的参数
//    userID----》 环信ID-----》 token
        Long uid = null;
        if(userID!=null){
            uid=userID;
        }else{
            if(huanxinID!=null){
                uid=huanxinID;
            }else{
                User user = userService.getUserByToken(token);
                uid = user.getId();
            }
        }
        return userService.queryUserInfoById(uid);
    }


}
